<%--
  -	Date: 2012-05-30
  -
  - Copyright (C) 2012 Jawa Software. All rights reserved.
  - 
  - 机构管理页面。
--%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<%@ page import="org.jawa.admin.util.ParamUtils,org.jawa.core.util.JawaGlobals,
	        	 org.jawa.core.JawaServer,
	        	 org.jawa.branch.*"
%>
<%@ page import="java.util.List,
                 java.util.Map,
                 java.util.HashMap"
%>
<%@ page import="org.apache.commons.lang.StringUtils"%>

<jsp:useBean id="webManager" class="org.jawa.admin.util.WebManager"  />
<% 
    webManager.init(request, response, session, application, out);
	String path = request.getContextPath();
%>

<%
	int range = ParamUtils.getIntParameter(request, "range", 20);
	int start = webManager.getPageIndex(range,"branch");
	
	List<Branch> branchs = webManager.getBranchManager().getBranchs(start,range);
	int branchCount = webManager.getBranchManager().getBranchCount();
	
	
	String brParentId = ParamUtils.getParameter(request,"brParentId");
	String brNo = ParamUtils.getParameter(request,"brNo");
	String brName = ParamUtils.getParameter(request,"brName");
	
	brParentId = StringUtils.trimToEmpty(brParentId);
	brNo = StringUtils.trimToEmpty(brNo);
	brName = StringUtils.trimToEmpty(brName);
	

	
	if (request.getParameter("search") != null && !request.getParameter("search").equals(""))
	{
		String[] formFields = new String[]{"br_id","user_name"};
		
		Map<String,String>  querys = new HashMap<String,String>();
		
		if(StringUtils.isNotEmpty(brParentId)){
			querys.put("brParentId",brParentId);
		}
		if(StringUtils.isNotEmpty(brNo)){
			querys.put("brNo",brNo);
		}
		if(StringUtils.isNotEmpty(brName)){
			querys.put("brName","%"+brName+"%");
		}
		
		branchs = webManager.getBranchManager().findBranchs(querys,start,range);
		branchCount = branchs.size();
	}
	
	
	request.setAttribute("branchs",branchs);
	request.setAttribute("branchCount",branchCount);
	request.setAttribute("range",range);
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>用户管理</title>
		<meta name="pageID" content="1201" />
		<link rel="stylesheet"  href="<%= path %>/styles/ztree/ztree.css">
	</head>
	<body>
		<div class="row-fluid">

			<ul class="breadcrumb">
				<li>
					您现在的位置:
				</li>
				<li>
					<a href="<%=path%>/admin/index.jsp">系统管理</a>
				</li>
				<li>
					<span class="divider">/</span>
				</li>
				<li class="active">
					部门管理
				</li>
			</ul>

			<div class="page-header hide">
				<h3>
					部门管理
				</h3>
			</div>

			<form action="index.jsp" method="post" class="form-search well">
				<div class="row-fluid ">
						<%
							String brParentName = "";
							if(StringUtils.isNotEmpty(brParentId)){
								Branch parent =  BranchManager.getInstance().getBranch(Long.valueOf(brParentId));
								brParentName = parent.getBrName();
							}
						%>
						<label class="query-label">
							上级部门:
						</label>
						<input type="hidden" id="brParentId" name="brParentId" value="<%=brParentId %>" />
						<input type="text" id="brParentName" name="brParentName" value="<%=brParentName %>" maxlength="15" class="" readonly="readonly" onclick="showMenu(); return false;" style="cursor: pointer;" />
						<label class="query-label">
							部门编号:
						</label>
						<input type="text" name="brNo" value="<%=brNo %>" class="input-small " >
						<label class="query-label">
							部门名称:
						</label>
						<input type="text" name="brName" value="<%=brName %>" class="input-small " >
						<input type="submit" name="search" class="btn btn-primary" value="查询">
				</div>
			</form>
			
			<div class="subnav">
			    <div class="btn-toolbar pull-left">
			        <%
			        	if("true".equals(request.getParameter("create")) && "true".equals(request.getParameter("success"))) 
			        	{
			        %>
						<p class="alert alert-success">
						 <a class="close" data-dismiss="alert" href="#">×</a> 
						    创建新部门成功!
						</p>
					<%  
						} 
					%>
			        <%
			        	if("true".equals(request.getParameter("update")) && "true".equals(request.getParameter("success"))) 
			        	{
			        %>
						<p class="alert alert-success">
						 <a class="close" data-dismiss="alert" href="#">×</a> 
						   修改部门成功!
						</p>
					<%  
						} 
			        %>
			        <%
		        	    if("true".equals(request.getParameter("delete")) && "true".equals(request.getParameter("success"))) 
			        	{
			        %>
						<p class="alert alert-success">
						 <a class="close" data-dismiss="alert" href="#">×</a> 
						   删除部门成功!
						</p>
					<%  
						} 
					%>
			    </div>
			    <div class="btn-toolbar pull-right">
					<a class="btn btn-success " href="branch-form.jsp"><i class="icon-user icon-white"></i>增加部门</a>
			    </div>
			</div>

				<display:table id="branch" name="branchs" partialList="true" 
					size="${branchCount}" pagesize="${range}" requestURI="index.jsp" export="false"
					class="table table-striped table-bordered table-condensed">
					<display:column title="部门编码">
						<c:out value="${branch.brNo}" />
					</display:column>
					<display:column title="部门名称">
						<c:out value="${branch.brName}" />
					</display:column>
					<display:column title="上级部门">
					    <c:if test="${not empty branch.parent}">
							<c:out value="${branch.parent.brName}" />
					    </c:if>
					</display:column>
					<display:column title="部门状态">
						<c:if test="${branch.brStatus=='0'}">正常</c:if>
						<c:if test="${branch.brStatus=='1'}">停用</c:if>
					</display:column>
					<display:column title="操作" style="width:15%;">
					    <div class="btn-group">
							<a href="#" onclick="return deleteBranch('${branch.brId }');" class="btn btn-mini"><i class="icon-trash "></i>删除</a>
							<a href="branch-form.jsp?edit=true&brId=${branch.brId}" class="btn btn-mini"><i class="icon-edit "></i>修改</a>
					    </div>
					</display:column>
				</display:table>

		</div>
		<div id="menuContent" class="menuContent" style="display:none; position: absolute;background-color: #EEE;">
			<ul id="branchTree" class="ztree" style="margin-top:0; width:210px;border: 1px solid #DDD; "></ul>
		</div>
		<script src="<%= path %>/scripts/bootstrap.js"></script>
		<script src="<%= path %>/scripts/jquery.ztree.js"></script>
		<script>
		var zNodes =[
						<%
						branchs = BranchManager.getInstance().getBranchs();
						int i = 0;
						int k = branchs.size();
						for(Branch branch : branchs){
							i++;
							String data = "{id:"+branch.getBrId()+", name: '"+branch.getBrName()+"' ";
							if(branch.getBrParentId()!=null){
								data +=",pId:"+branch.getBrParentId()+"";
							}
							data +="}";
							if(i!=k){
								data +=",";
							}
							out.println(data);
						}
						%>		     		
				];
				var setting = {
					view: {
						dblClickExpand: false
					},
					data: {
						simpleData: {
							enable: true
						}
					},
					callback: {
						beforeClick: beforeClick,
						onClick: onClick
					}
				};
				
				function beforeClick(treeId, treeNode) {}
				
				function onClick(e, treeId, treeNode) {
					var zTree = $.fn.zTree.getZTreeObj("branchTree"),
					nodes = zTree.getSelectedNodes(),
					v = "";
					k = "";
					nodes.sort(function compare(a,b){return a.id-b.id;});
					for (var i=0, l=nodes.length; i<l; i++) {
						v += nodes[i].name + ",";
						k += nodes[i].id + ",";
					}
					if (v.length > 0 ) v = v.substring(0, v.length-1);
					if (k.length > 0 ) k = k.substring(0, k.length-1);
					var brNameObj = $("#brParentName");
					brNameObj.attr("value", v);
					var brIdObj = $("#brParentId");
					brIdObj.attr("value", k);
					$("#menuContent").fadeOut("fast");
				}

				function hideMenu() {
					$("#menuContent").fadeOut("fast");
					$("body").unbind("mousedown", onBodyDown);
				}
				function onBodyDown(event) {
					if (!(event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
						hideMenu();
					}
				}
				function showMenu() {
					var cityObj = $("#brParentName");
					var cityOffset = $("#brParentName").offset();
					$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
					$("body").bind("mousedown", onBodyDown);
				}
				$().ready(function() {
					$.fn.zTree.init($("#branchTree"), setting, zNodes);
				});
											
			function deleteBranch(brId){
				if(confirm('你确定要删除当前部门吗?')){
					window.location.href='branch-form.jsp?delete=true&brId='+brId;
				}
			}
		</script>
		
	</body>